Reputation: 176
I have a limited amount of c experience and virtually no experience using threads. If there is a simple way to wrap the "forever" function into a thread and allow main() to continue running, please let me know how it can be done. This example is totally impractical of course but if I can get it to work, I think its a good starting point. Maybe its not even possible? But would like to know. Thanks!
//
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include "stdafx.h"
void foreverFunction(); // function prototype
int myNumber=1;
void main()
{
foreverFunction(); // call forever (looping function), would like get it running as a separate thread so that the code below can continue.
while (myNumber != 0 ){
printf("Enter a number (0 to exit): ");
scanf("%d",&myNumber);
printf("Entered: %d\n",myNumber);
}
}
// function below should report the current value of myNumber every second
void foreverFunction(){
while (myNumber>0){
printf ("\nThis will run until the user enters 0, Last value entered was %d\n",myNumber);
Sleep(1000);
}
}
Upvotes: 0
Views: 120
Reputation: 176
Here is my updated code (from the example above) which is working and doing exactly what I expect. Thanks to all.
#include <windows.h>
#include <WinBase.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include "stdafx.h"
#include <process.h>
int myNumber=1;
// function below should report the current value of myNumber every second
unsigned __stdcall foreverFunction(void* pArguments){
printf("The thread is started...\n");
while (myNumber>0){
printf ("Selected number: %d\n",myNumber);
Sleep(1000);
}
printf("The thread as exited... \n");
return NULL;
}
void main()
{
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, &foreverFunction, NULL, 0, &threadID );
while (myNumber != 0 ){
printf("Enter a number (0 to exit): ");
scanf("%d",&myNumber);
printf("Entered: %d\n",myNumber);
}
}
The Console Output:
Enter a number (0 to exit): The thread is started...
Selected number: 1
Selected number: 1
Selected number: 1
Selected number: 1
Selected number: 1
3
Entered: 3
Enter a number (0 to exit): Selected number: 3
Selected number: 3
Selected number: 3
Selected number: 3
Selected number: 3
Selected number: 3
Selected number: 3
54
Entered: 54
Enter a number (0 to exit): Selected number: 54
Selected number: 54
Selected number: 54
Selected number: 54
Upvotes: 0
Reputation: 1223
for a simple program like yours you can use beginthread()
_beginthread(foreverfunction, 0, NULL);
the first argument is a function pointer (the name of your function). The second is the stack size and can be 0. The third is a parameter you want give your function (more specific, a pointer to the parameter). If you have multiple arguments you have to pack them into a struct
another function you could use in Windows is CreateThread() for Linux (POSIX) see haris answer
Upvotes: 1
Reputation: 12272
Edited
If you're working on linux, you can use the pthread
for this purpose. For your example, to keep the function
running forever, you can do something like
pthread_t thread1;
pthread_create (&thread1, NULL, (void *) &foreverFunction);
pthread_join(thread1, NULL);
Now, your function won't exit until main
exits. This is taken care by the pthread_join()
call.
Upvotes: 0