How do I modify CreateThread function? - c++

Like my title says, I'm trying to modify the CreateThread function. I want it to report the sequence number of the thread using the loop.
I'm fairly new to programming in C++ and am unsure which parameter needs to be altered. I suspect it may be the 'lpParameter' but after looking on microsofts website (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx) I still do not understand how the parameters work.
So far I have:
int int_tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread[numThreads];
int tNum[10];
for (int i = 0; i < numThreads; i++)
{
tNum[i] = i;
hThread[i] =
CreateThread(NULL, 0, helloFun, NULL, 0, NULL);
} WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);
return 0;
}
Which produces a blank thread.
EDIT:
Sorry for the confusion guys. I have declared the thread routine:
const int numThreads = 4;
DWORD WINAPI helloFun(LPVOID pArg)
{
printf("Hello Thread \n");
return 0;
}

It's not clear what you are trying to do, but it is clear that you are calling WaitForMultipleObjects in the wrong place.
You are trying to wait for numThreads after only creating one thread. The rest of the hThread array is still uninitialized, and passing wild handles to WaitForMultipleObjects is a terrible idea.
Move the wait call outside the loop.

Your thread routine must be declared
DWORD name (LPVOID whatever)
Whatever is passed as lpParameter is passed to the thread. In this case, cast integer to pointer and back.

As you mentioned, if you want to pass a parameter to the thread, then you use the lpParameter. It expects a pointer, so you could pass, for instance, &i, although that's not exactly a good idea. You could create a pointer to int, and pass it, and then delete after WaitForMultipleObjectsreturns.
As Ben said, you are calling WaitForMultipleObjects in the wrong place.
Something like this should work:
int int_tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread[numThreads];
int tNum[10];
int *parameters[numThreads];
for (int i = 0; i < numThreads; i++)
{
tNum[i] = i;
parameters[i] = new int(i);
hThread[i] = CreateThread(NULL, 0, helloFun, (void *)parameters[i], 0, NULL);
}
WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);
for (int i = 0; i < numThreads; i++)
delete parameters[i];
return 0;
}
You will have to cast the lpParameter to int in your helloFun.

Try something more like this:
const int maxThreads = 4;
DWORD WINAPI helloFun(LPVOID pArg)
{
int num = * (int*) pArg;
printf("Hello from Thread %d\n", num);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread[maxThreads];
int tNum[maxThreads];
int numThreads = 0;
for (int i = 0; i < maxThreads; i++)
{
tNum[numThreads] = i+1;
hThread[numThreads] = CreateThread(NULL, 0, &helloFun, &tNum[numThreads], 0, NULL);
if (hThread[numThreads] == NULL)
{
printf("Unable to create a thread! Error: %u\n", GetLastError());
break;
}
++numThreads;
}
if (numThreads > 0)
{
WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);
for (int i = 0; i < numThreads; i++)
CloseHandle(hThread[i]);
}
return 0;
}

Related

Call join child pthread in main function

I have the test code:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t th_worker, th_worker2;
void * worker2(void *data) {
for(int i = 0; i< 1000000; i++){
printf("thread for worker2----%d\n", i);
usleep(500);
}
}
void * worker(void *data){
pthread_create(&th_worker2, NULL, worker2, data);
for(int i = 0; i< 100; i++){
printf("thread for worker-----%d\n", i);
usleep(500);
}
}
void join(pthread_t _th){
pthread_join(_th, NULL);
}
In main() function, If I call join(the_worker2):
int main() {
char* str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void*) str);
/* problem in here */
join(th_worker2);
return 1;
}
--> Segment Fault error
Else, i call:
join(the_worker);
join(th_worker2);
---> OK
Why have segment fault error in above case?
Thanks for help !!!
If you posted all your code, you have a race condition.
main is synchronized with the start of worker but not worker2.
That is, main is trying to join th_worker2 before worker has had a chance to invoke pthread_create and set up th_worker2 with a valid [non-null] value.
So, th_worker2 will be invalid until the second pthread_create completes, but that's already too late for main. It has already fetched th_worker2, which has a NULL value and main will segfault.
When you add the join for th_worker, it works because it guarantees synchronization and no race condition.
To achieve this guarantee without the join, have main do:
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
while (! th_worker2)
usleep(100);
/* problem in here */
join(th_worker2);
return 1;
}
An even better way to do this is to add an extra variable. With this, the first loop is not needed [but I've left it in]:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int worker_running;
pthread_t th_worker;
int worker2_running;
pthread_t th_worker2;
void *
worker2(void *data)
{
// tell main we're fully functional
worker2_running = 1;
for (int i = 0; i < 1000000; i++) {
printf("thread for worker2----%d\n", i);
usleep(500);
}
return NULL;
}
void *
worker(void *data)
{
// tell main we're fully functional
worker_running = 1;
pthread_create(&th_worker2, NULL, worker2, data);
for (int i = 0; i < 100; i++) {
printf("thread for worker-----%d\n", i);
usleep(500);
}
return NULL;
}
void
join(pthread_t _th)
{
pthread_join(_th, NULL);
}
int
main()
{
char *str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void *) str);
// give worker enough time to properly start worker2
// NOTE: this not necessarily needed as loop below is better
while (! th_worker2)
usleep(100);
// give worker2 enough time to completely start
while (! worker2_running)
usleep(100);
/* problem in here (not anymore!) */
join(th_worker2);
return 1;
}

threads are halted by function send() or recv() of ACE_SOCK_Stream?

I am using pthread + ACE to write a fake client.
this client has 3 threads, and each thread could send and receive message endlessly by using ACE. however, these threads always be halted by the function send() or recv(). that is, the thread would exit if there is something wrong with sending or receiving, unfortunately, I don't know what is the error, and I could not catch it, too. the codes are:
struct thread_data {
int thread_id;
string ip;
uint32_t port;
uint32_t timeout;
};
std::vector<struct thread_data> m_thread;
void * test_fun1(void * threadid)
{
struct thread_data * tmp_thread_data = (struct thread_data *)threadid;
long tmp_threadid = (long)tmp_thread_data->thread_id;
string tmp_ip = tmp_thread_data->ip;
uint32_t tmp_port = tmp_thread_data->port;
uint32_t tmp_timeout = tmp_thread_data->timeout;
ACE_INET_Addr addr(tmp_port, tmp_ip.c_str());
ACE_Time_Value timeout(0, tmp_timeout * 1000);
ACE_SOCK_Connector connector;
ACE_SOCK_Stream peer;
// connect
if(connector.connect(peer, addr, &timeout) != 0)
pthread_exit((void *) threadid);
// send msg
while (1)
{
ssize_t tmp_ret1 = peer.send("hello world", 12);
if (tmp_ret1 <= 0)
continue;
char tmp_buf[1024] = '\0';
ssize_t tmp_ret2 = peer.recv(tmp_buf, 1024, &timeout);
if (tmp_ret2 <= 0)
continue;
else
fprintf(stderr, "recv:%s\n", tmp_buf);
}
// close
peer.close();
pthread_exit((void *) threadid);
}
int main(int argc, char *argv[])
{
std::vector<pthread_t> threads;
pthread_attr_t attr;
int rc;
int i = 0;
void * status;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// thread create
int tmp_num = 3;
for(i = 0; i < tmp_num; i++)
{
pthread_t tmp_thread_handler;
struct thread_data tmp_thread_info;
tmp_thread_info.thread_id = i;
tmp_thread_info.ip = "127.0.0.1";
tmp_thread_info.port = 8001;
tmp_thread_info.timeout = 100;
rc = pthread_create(&tmp_thread_handler, NULL, test_fun1, (void *)&tmp_thread_info);
if (rc != 0)
return -1;
threads.push_back(tmp_thread_handler);
m_thread.push_back(tmp_thread_info);
}
// thread start
pthread_attr_destroy(&attr);
for(i = 0; i < tmp_num; i++)
{
rc = pthread_join(threads[i], &status);
if (rc != 0)
return -1;
}
pthread_exit(NULL);
return 0;
}
what should I do if I want to send and receive message endlessly? any help would be appreciated!
You have a significant scope-lifetime issue in this code. You're sending a data block to your threads that is created/destroyed with each loop iteration. Leaving out the side-lines:
// thread create
int tmp_num = 3;
for(i = 0; i < tmp_num; i++)
{
pthread_t tmp_thread_handler;
struct thread_data tmp_thread_info;
// --- snip ----
rc = pthread_create(&tmp_thread_handler, NULL, test_fun1, (void *)&tmp_thread_info);
// --- snip ----
}
Whether the resulting block is copied into the underlying info vectors or not is not relevant. Once the loop slings around, the object is destroyed, and with it the std::string member variable ip. It may reside in the same memory space (the struct), but the reallocation of the string almost certainly will not. In other words, you're invoking undefined behavior.
A possible resolution to this is to use a smart pointer for the structure, passing its get() member to the thread, and pushing said-same smart pointer into your info vector ( retooled for smart pointers). I personally prefer a simpler approach, namely allocating both vectors before the threads are kicked off, thereby fixing their "innards" for by-address usage. From there you can send the structure address since the vector holds it for you:
// thread create
size_t tmp_num = 3;
m_thread.resize(tmp_num);
threads.resize(tmp_num);
for(size_t i = 0; i < tmp_num; i++)
{
m_thread[i].thread_id = i;
m_thread[i].ip = "127.0.0.1";
m_thread[i].port = 8001;
m_thread[i].timeout = 100;
rc = pthread_create(&threads[i], NULL, test_fun1, &m_thread[i]);
if (rc != 0)
return -1;
}
Regarding other issues, in your thread proc this doesn't compile:
char tmp_buf[1024] = '\0';
But that's pretty minor compared to the undefined behavior you're having.

C++ Wait all threads to finish

#include <stdio.h>
#include <process.h>
#include <wtypes.h>
typedef unsigned int (__stdcall * THREAD_FUN_TYPE)(void *);
int ThreadIp(void* param)
{
while(true)
{
printf("I'm runing!\n");
}
return 0;
}
int main()
{
int iThreadNum=100;
HANDLE* phThreads = new HANDLE[iThreadNum];
for (int i=0;i<iThreadNum;++i)
{
phThreads[i]=(HANDLE*)_beginthreadex(NULL, 0, (THREAD_FUN_TYPE)ThreadIp,NULL, NULL, NULL);
}
int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);
printf("End!\n");
return 0;
}
I want the program will halt at WaitForMultipleObjects until all thread are end(Not until all thread are created successfully).But the program will not halt at WaitForMultipleObjects,while all threads are still running. So I try to use SetEvent,but still the same problem:
int iThreadNum=100;
HANDLE* phThreads = new HANDLE[iThreadNum];
for (int i=0;i<iThreadNum;++i)
{
phThreads[i]=CreateEvent(NULL, FALSE, FALSE,NULL);
ResetEvent(phThreads[i]);
}
int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);
You should wait on the thread handles, not the unrelated events:
Try something like this:
int iThreadNum=100;
HANDLE* phThreads = new HANDLE[iThreadNum];
for (int i=0;i<iThreadNum;++i)
{
m_iCurThreadNum=i;
phThreads[i] = _beginthreadex(...);
}
int nIndex = ::WaitForMultipleObjects(iThreadNum,phThreads,1,INFINITE);
Does it work if you have fewer threads? The manual says you need to do extra work if you have more than MAXIMUM_WAIT_OBJECTS, specifically
nCount [in] The number of object handles in the array pointed to by
lpHandles. The maximum number of object handles is
MAXIMUM_WAIT_OBJECTS. This parameter cannot be zero.
See here for a discussion.
It might be worth checking what the wait function has returned too.
I would allocate a struct before calling _beginthreadex and pass the pointer to the struct through the threads parameter and have the struct contain a bool which is set by the thread when it's done.
struct ThreadStruct{
bool Done;
char* ParamData;
int ParamDataSize;
};
int ThreadIp(void* param)
{
ThreadStruct* ts = (ThreadStruct*)param;
while(true)
{
printf("I'm runing!\n");
}
ts->Done = true;
return 0;
}
int main()
{
int iThreadNum=100;
HANDLE* phThreads = new HANDLE[iThreadNum];
ThreadStruct* structs = new ThreadStruct[iThreadNum];
for (int i=0;i<iThreadNum;++i)
{
ZeroMemory(structs[i], sizeof(ThreadStruct));
phThreads[i]=(HANDLE*)_beginthreadex(NULL, 0, (THREAD_FUN_TYPE)ThreadIp, structs[i], NULL, NULL);
ResetEvent(phThreads[i]);
}
for(unsigned int i=0; i<iThreadNum;){
if(!structs[i]->Done) i=0;
else i++;
}
printf("End!\n");
return 0;
}

"Un-casting" from (void *) and de-referencing to char array

I have almost completed an homework assignment where I am required to use pthreads. I have figured out the pthreads. The only problem I have left is figuring out how to pass multiple arguments to threads through pthread_create().
I need to pass two chars to the thread. I have to cast them to (*void) to use with pthread_create(). I can pass them, but I can't figure out how to get the values from *parameter in the function.
void *my_function(void *parameter) {
/* ATTEMPT 1 - DOESN'T WORK */
//char* arguments[2];
//arguments = (char * [2]) parameter;
/*Error message:
error: ISO C++ forbids casting to an array type char* [2] [-fpermissive]
error: incompatible types in assignment of char** to char*[2]
*/
/* ATTEMPT 2 - DOESN'T WORK */
//char *my_data = (char *)parameter;
//my_data is blank when I try to use cout to check it's values
/* What I need to do is get those two chars from the array and print them out as part of this thread function */
pthread_exit(NULL);
}
int main(int argc, char **argv) {
char duration = '5'; //in reality, this value is taken from argv but I am leaving that out for brevity
pthread_t threads[3];
for(int i=0; i < 3; i++){
char thread_args[2] = {i, duration};
//create thread with arguments passed in
int results = pthread_create(&threads[i], NULL, my_function, (void *) &thread_args);
//testing for pthread error
if (results){
printf("ERROR; return code from pthread_create() is %d\n", results);
exit(-1);
}
}
/* Wait for all threads to complete */
for (int j=0; j < num_threads; j++) { // https://computing.llnl.gov/tutorials/pthreads/
pthread_join(threads[j], NULL);
}
/* some information prints here that is unrelated to my problem (the date, time, etc) */
pthread_exit(NULL);
}
I was able to pass one value through with no problem. Any suggestions?
The closest existing question I could find was this but I still am having no luck: Converting from void* to char ** in C
Thank you!
Note that within this loop:
for(int i=0; i < 3; i++){
char thread_args[2] = {i, duration};
int results = pthread_create(&threads[i], NULL, my_function, (void *)
...
}
thread_args is a local array with automatic storage duration, lifetime of which is tied to each iteration so there is a chance that the memory where you store these arguments might be freed before the thread will access it, which would lead to undefined behavior in that case.
Much better approach would be to create a structure, that you would use to pass the data to this thread:
typedef struct {
char duration;
int num;
} ThreadData;
Then your code could look like this:
void *my_function(void *parameter) {
// retrieve and print the thread data:
ThreadData* td = (ThreadData*) parameter;
printf("num = %d, duration = %c\n", td->num, td->duration);
delete td;
return NULL;
}
int main(int argc, char **argv) {
char duration = '5';
pthread_t threads[3];
for (int i = 0; i < 3; i++) {
// create structure that will be passed to thread:
ThreadData* td = new ThreadData;
td->duration = duration;
td->num = i;
//create thread with arguments passed in:
int ret = pthread_create(&threads[i], NULL, my_function, (void *) td);
//testing for pthread error:
if (ret) {
printf("ERROR; return code from pthread_create() is %d\n", ret);
exit(-1);
}
}
// wait for all threads to complete:
for (int i = 0; i < 3; i++) {
pthread_join(threads[i], NULL);
}
exit(0);
}
Also note that to end thread's execution it is better to use return than pthread_exit since with return it is guaranteed that variables within thread's routine will be destroyed and stack will be unwound. For more information see return() versus pthread_exit() in pthread start functions

Efficiently passing parameters to function in another thread in C++

As far as I can see, a pointer to structure can be used. But I am wondering, is there any more efficient or elegant way to do that? At least when a structure is being used, it is not easy to see what are the parameters used by the function.
Thanks for any insightful answer.
Here's a small example, that uses WIN32 API:
#include <windows.h>
#include <stdio.h>
struct PARAMS
{
int i;
char* msg;
};
DWORD WINAPI myThread(void* parameter)
{
PARAMS* params = (PARAMS*)parameter;
printf("Received parameters: i = %d, msg = '%s'\n", params->i, params->msg);
return 0;
}
int main(int argc, char* argv[])
{
char msg[] = "Hi there.";
PARAMS params;
params.i = 1;
params.msg = msg;
HANDLE threadHandle = CreateThread(NULL, 0, myThread, &params, 0, NULL);
WaitForSingleObject(threadHandle, INFINITE);
return 0;
}
You say, that "it is not easy to see what are the parameters used by the function". Well it depends on situation. If you don't consider it "elegant" enough, you should leave some helpful comment there at least... if you are using good naming and trying to write code, that is self-documenting, then using of structure will be just fine.
Here's an example of wrapping CreateThread so that programmer that uses your code doesn't have to know that you are using some structure:
#include <windows.h>
#include <stdio.h>
class MyWrapper
{
private:
struct PARAMS
{
int i;
char* msg;
};
static DWORD WINAPI myThread(void* parameter)
{
PARAMS* params = (PARAMS*)parameter;
printf("Received parameters: i = %d, msg = '%s'\n", params->i, params->msg);
delete params;
return 0;
}
public:
HANDLE createThread(int i, char* msg)
{
PARAMS* params = new PARAMS;
params->i = i;
params->msg = msg;
return CreateThread(NULL, 0, MyWrapper::myThread, params, 0, NULL);
}
};
int main(int argc, char* argv[])
{
MyWrapper mw;
char msg[] = "Hi there.";
HANDLE threadHandle = mw.createThread(1, msg);
WaitForSingleObject(threadHandle, INFINITE);
return 0;
}
Here is a small example if you want to pass a single parameter to a thread function in Win32 API
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
DWORD WINAPI funHello(void *x)
{
int c = (int*)x;
printf("\n Thread No: %d\n",c);
// Do some work , call some function, etc.
return 0;
}
int main()
{
HANDLE myhandle;
DWORD threadId;
int c = 1;
myhandle = CreateThread(NULL, 0, funHello, (void *)c, 0, &threadId);
if (myhandle == NULL)
{
printf("Create Thread Failed. Error no: %d\n", GetLastError);
}
WaitForSingleObject(myhandle, INFINITE);
printf("\n Main Hello...\n");
CloseHandle(myhandle);
return 0;
}