标签: createthread

C,如何使用pthread_create函数创建线程

我正在为一个调度队列制作一个ac文件,该队列获取一个任务并将其放入一个链接列表的队列中.为了做到这一点,我需要使用创建线程

pthread_t cThread;
if(pthread_create(&cThread, NULL, work, param)){
    perror("ERROR creating thread.");
}
Run Code Online (Sandbox Code Playgroud)

但是我需要创建另一个作为'work'和'param'变量的函数作为create function的参数.我的朋友告诉我,我只需要在无限循环的工作函数中放入任何代码,这样线程就不会死.任何人都可以解释每个参数进入pthread_create函数 - 特别是对于workparam?我搜索谷歌这个,但大多数教程都很难理解这个概念......

c multithreading pthreads function createthread

18
推荐指数
2
解决办法
5万
查看次数

CreateThread函数的多个参数

当我使用CreateThread API方法时,当我想传递LPVOID lpParameter传递的多个参数时,我需要做什么?

c++ createthread

7
推荐指数
2
解决办法
8648
查看次数

如何将整数传递给CreateThread()?

如何将int参数传递给CreateThread回调函数?我试试看:

DWORD WINAPI mHandler(LPVOID sId) {
...
arr[(int)sId]
...
}

int id=1;
CreateThread(NULL, NULL, mHandler, (LPVOID)id, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

但我收到警告:

warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'int'
warning C4312: 'type cast' : conversion from 'int' to 'LPVOID' of greater size
Run Code Online (Sandbox Code Playgroud)

c++ int createthread

7
推荐指数
1
解决办法
9563
查看次数

C++中的函数指针

我使用CreateThread函数在C++中编写类似C#BackgroundWorker的类.我的代码:

BackgroundWorker.h:

class BackgroundWorker
{
    private :
        HANDLE _threadHandle;
        unsigned int _threadCallcounter;
        DWORD _threadID;
    public:
        BackgroundWorker();
        ~BackgroundWorker();
        virtual DWORD WINAPI Function(LPVOID vpPram);
}
Run Code Online (Sandbox Code Playgroud)

BackgroundWorker.cpp:

#include "BackgroundWorker.h"
void BackgroundWorker::DoWork()
{
    this->_threadHandle = CreateThread(NULL,
        0,this->Function,&this->_threadCallcounter,
        0, &this->_threadID); // !!!***This part throws an error***!!!
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了另一个派生自BackgroundWorker的类:

ListenThread.cpp:

class ListenThread :public BackgroundWorker
{
    DWORD WINAPI Function(LPVOID vpPram)
    {
        //DO somthing...
        return 0;
    }
};
Run Code Online (Sandbox Code Playgroud)

但该行给了我以下错误:

非标准语法; 使用'&'创建指向成员的指针

c# c++ multithreading backgroundworker createthread

5
推荐指数
1
解决办法
241
查看次数

将参数传递给CreateThread

我在调用CreateThread时将类引用作为参数传递给ThreadProc时遇到问题.这是一个演示我遇到的问题的示例程序:

program test;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows, Dialogs;

type
    TBlah = class
        public
        fe: Integer;
    end;

function ThreadProc(param: Pointer) : DWORD;
begin
    ShowMessage(IntToStr(TBlah(param).fe));

    Result := 0;
end;

var
    tID: DWORD;
    handle: THandle;
    b: TBlah;
begin
    b := TBlah.Create;
    b.fe := 54;

    handle := CreateThread(nil, 0, @ThreadProc, Pointer(b), 0, tID);

    WaitForSingleObject(handle, INFINITE); 
end.
Run Code Online (Sandbox Code Playgroud)

调用ShowMessage弹出一个消息框,里面有类似的东西245729105,54不像我期望的那样.

这可能只是对Delphi如何工作的基本误解,所以有人可以告诉我如何使其正常工作吗?

delphi parameter-passing delphi-2010 createthread

3
推荐指数
2
解决办法
6825
查看次数

难以通过pthread_create传递struct

我有一个结构数组,我打算在for循环中将数组的每个元素传递给单独的pthreads.

这是我的结构:

struct arrayData{
int *a;
int *b;
int up, low;
}
Run Code Online (Sandbox Code Playgroud)

}

这是指向第一个结构和一个malloc的指针(dunno,如果我完全了解这里发生的事情):

struct arrayData * instance;
        instance = malloc(sizeof(struct arrayData)*n);
Run Code Online (Sandbox Code Playgroud)

这是我对pthread_create的调用:

pthread_create( &thread[i], NULL, add, (void *)instance[i]);
Run Code Online (Sandbox Code Playgroud)

对于该行,我收到消息"无法转换为指针类型".

这条线有什么问题?

c struct pthreads createthread

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

在线程,C++中写入文件

我写了一个程序,它应该在线程中写入"1"三秒钟.但是当我尝试调试或添加控件输出时,我意识到通常不会创建线程(控制台中没有输出或调试点的成就).当我检查的返回值CreateThread(),它的确定.我读了关于I/O的登录文件,但我想我不需要它.我想要一个有两个线程的程序; 一个写"1"三秒钟,第二个写"2"三秒钟.然后,比较的结果.如果"1"和"2"中输入文件混合无所谓.

#include <iostream>
#include <fstream>
#include <windows.h>
#include <stdio.h>
#include <WinBase.h>
#include <ctime>

#define NTHREAD 2

std::ofstream myfile;

DWORD WINAPI fce1 (LPVOID a){
    time_t timerStart, timerNow;
    time(&timerStart);
    timerNow = timerStart;
    while((timerNow - timerStart) < 3)
    {
        myfile << "1";
        myfile.flush();
        time(&timerNow);
    }
    return 0;
}


int main()
{
    HANDLE threads[NTHREAD];
    DWORD dwThreads[NTHREAD];

    myfile.open("file.txt");
    threads[0] = CreateThread(NULL, 0, fce1, NULL, 0, &dwThreads[0]);
    if (threads[0] == NULL){
        printf("Error\n");
    }

    myfile.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading ofstream createthread

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

CreateThread似乎表现得像fork()

我有这个功能

int APIENTRY _tWinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPTSTR    lpCmdLine,
                 int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG     msg;
HACCEL  hAccelTable;
int     i;
DWORD   dwThreadIdArray[NUM_THREADS]; //edited after first post
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
PARAM_PASSED *paramPassed = NULL;
std::ostringstream ss;
std::wstring str;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LSP3, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
    return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LSP3));

// Initialize GDI+ 
GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL); 

// …
Run Code Online (Sandbox Code Playgroud)

c++ fork visual-studio-2010 createthread

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

多线程程序只执行最后创建的线程?

我创建了4个线程,但是当我执行这个程序时,我将其作为输出:

Thread #: 1  
Thread #: 2 
Thread #: 3 
Thread #: 3 
Thread #: 4 
Thread #: 4 
Thread #: 4
Thread #: 5 
Thread #: 5 
Thread #: 5
.
.
.
Thread #: 5 
Thread #: 5 
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. 我只创建了4个线程,为什么它显示线程#:5
  2. 创建的所有4个线程应该按顺序运行,但为什么它只运行最后创建的线程?

源代码:

#include<windows.h>


HANDLE ThreadHandle[4];
DWORD dwThreadId[4];

DWORD WINAPI ThreadFunction(LPVOID param)
{

    while (1)
    {

        printf("Thread #: %d\n", *((int*)param));
    }

    Sleep(10);

    return 0;
}

int main()
{
    static int i = 0;
    for (i = 1; i <= …
Run Code Online (Sandbox Code Playgroud)

c c++ multithreading createthread

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