小编kvv*_*kvv的帖子

多线程工作速度较慢

美好的一天!

我正在尝试学习python中的多线程功能,我编写了以下代码:

import time, argparse, threading, sys, subprocess, os

def item_fun(items, indices, lock):
    for index in indices:
        items[index] = items[index]*items[index]*items[index]

def map(items, cores):  

    count = len(items)
    cpi = count/cores
    threads = []
    lock = threading.Lock()
    for core in range(cores):
        thread = threading.Thread(target=item_fun, args=(items, range(core*cpi, core*cpi + cpi), lock))
        threads.append(thread)
        thread.start()
    item_fun(items, range((core+1)*cpi, count), lock)
    for thread in threads:
        thread.join()


parser = argparse.ArgumentParser(description='cube', usage='%(prog)s [options] -n')
parser.add_argument('-n', action='store', help='number', dest='n', default='1000000', metavar = '')
parser.add_argument('-mp', action='store_true', help='multi thread', dest='mp', default='True')
args …
Run Code Online (Sandbox Code Playgroud)

python multithreading

2
推荐指数
1
解决办法
163
查看次数

在线程例程函数中没有调用局部变量析构函数?

我用vs2013编写了简单的代码,它的工作原理很奇怪:

#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <cstdint>
#include <tchar.h>

class A
{
public:
    explicit A(uint8_t byte) : mByte(byte) {}
    ~A() { _tprintf(_T("A::~A(%x)\n"), mByte); }
private:
    uint8_t mByte;
};

unsigned WINAPI threadRoutine(void*)
{
    A a0(0x41);
    _endthreadex(0);
    return 0;
}

int _tmain(int argc, TCHAR *argv[])
{
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, threadRoutine, NULL, 0, NULL);
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出为空,这意味着没有为局部变量a0调用A-dtor?

我的代码中是否有一些错误?

如果在函数返回后没有调用局部变量析构函数,如何在线程例程函数中维护RAII?

c++ winapi multithreading

0
推荐指数
1
解决办法
137
查看次数

标签 统计

multithreading ×2

c++ ×1

python ×1

winapi ×1