Get thread id of main thread - multithreading

I am writing a plugin. I am only allowed to run code from a spawned main thread. I can't run code on the main thread.
From my thread, is it possible to get the thread id of the main thread?
I thought to do OpenProcess like this below - however it seems overkill, as my process of the thread is the same.
/* CAUTION: ONLY WIN32
* get the threadId of the main thread of a target process
*
* params:
* DWORD pId processId of the target process
*
* return:
* Success threadId
* Error NULL
*/
DWORD GetMainThreadId(DWORD pId)
{
LPVOID lpThId;
_asm
{
mov eax, fs:[18h]
add eax, 36
mov [lpThId], eax
}
HANDLE hProcess = OpenProcess(PROCESS_VM_READ, FALSE, pId);
if(hProcess == NULL)
return NULL;
DWORD tId;
if(ReadProcessMemory(hProcess, lpThId, &tId, sizeof(tId), NULL) == FALSE)
{
CloseHandle(hProcess);
return NULL;
}
CloseHandle(hProcess);
return tId;
}
Is there any better way?

First of all you need to define what you mean by the main thread. The system itself has no such concept.
Typically though, what you mean when you say the main thread, is the thread which created the main application window. So, find the main application window. And then pass that window to GetWindowThreadProcessId.

Related

How to cancel a thread from another thread (Glib threads)?

I am developing an application library using GTK and the functions for threads in GLib. I have a thread (from now on will be called thread A) that is created when I hit an "ok" button in a certain graphical window. Thread A starts doing some heavy tasks. Another button named "cancel" is available to stop and finish thread A at any moment.
My aim is to code a function for the thread created when I hit the "cancel" button (thread B) that has the ability to end the thread A.
I create thread A with the function g_thread_create. However I cannot find any function similar to g_thread_cancel to stop thread A using thread B. Is this possible or cannot be done?
Thank you so much for any kind of information provided.
You might want to consider using GTask to run your task in a thread, rather than using a manually-created thread. If you use g_task_run_in_thread(), the operation will run in a separate thread automatically.
GTask is integrated with GCancellable, so to cancel the operation you would call g_cancellable_cancel() in the callback from your ‘Cancel’ button.
As OznOg says, you should treat the GCancellable as a way of gently (and thread-safely) telling your task that it should cancel. Depending on how your long-running task is written, you could either check g_cancellable_is_cancelled() once per loop iteration, or you could add the GSource from g_cancellable_source_new() to a poll loop in your task.
The advice about using threads with GLib is probably also relevant here.
I have developed a code that is able to cancel a thread from another, both of them created from a main one. The code works correctly according to my tests:
#include <pthread.h>
#include <stdio.h>
/* these variables are references to the first and second threads */
pthread_t inc_x_thread, inc_y_thread;
/* this function is run by the first thread */
void *inc_x(void *x_void_ptr)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
/* increment x to 100 */
int *x_ptr = (int *)x_void_ptr;
while(++(*x_ptr) < 100000000);
printf("x increment finished\n");
/* the function must return something - NULL will do */
return NULL;
}
/* this function is run by the second thread */
void *inc_y(void *x_void_ptr)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
/* increment y to 100 */
int *x_ptr = (int *)x_void_ptr;
pthread_cancel(inc_x_thread);
while(++(*x_ptr) < 100);
printf("y increment finished\n");
return NULL;
}
/* this is the main thread */
int main()
{
int x = 0, y = 0;
void *res;
/* show the initial values of x and y */
printf("x: %d, y: %d\n", x, y);
/* create a first thread */
if(pthread_create(&inc_x_thread, NULL, inc_x, &x)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
/* create a second thread */
if(pthread_create(&inc_y_thread, NULL, inc_y, &y)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
/* wait for the first thread to finish */
if(pthread_join(inc_x_thread, &res)) {
fprintf(stderr, "Error joining thread\n");
return 2;
}
if (res == PTHREAD_CANCELED)
printf(" thread was canceled\n");
else
printf(" thread wasn't canceled\n");
/* wait for the second thread to finish */
if(pthread_join(inc_y_thread, &res)) {
fprintf(stderr, "Error joining thread\n");
return 2;
}
if (res == PTHREAD_CANCELED)
printf(" thread was canceled\n");
else
printf(" thread wasn't canceled\n");
/* show the results*/
printf("x: %d, y: %d\n", x, y);
return 0;
}
You can compile the code by using: gcc example.c -lpthread
However, as OznOg and Philip Withnall have said, this is not the correct way of cancelling a thread. It is only a quick way of doing it that might not work in some specific situations. A better and safer way is to gently ask the thread to stop itself.

Solaris thr_join vs posix pthread_join

In Solaris, thr_join documentation states the following:
int thr_join(thread_t thread, thread_t *departed, void
**status);
If the target thread ID is 0, thr_join() finds and returns
the status of a terminated undetached thread in the process.
Is POSIX pthread_join equivalent?
int pthread_join(pthread_t thread, void **status);
suspends processing of the calling thread until the target thread completes
How can I use pthread_join in case of thr_join when I would like to know which child thread have terminated among many.
Is there any other alternative?
In other words, if a parent thread spawns N child threads, how do the parent thread know by polling or something else which thread has exited / terminated?
Is POSIX pthread_join equivalent?
Yes, it's equivalent. Well, close enough. You can see the differences in the implementation:
int
thr_join(thread_t tid, thread_t *departed, void **status)
{
int error = _thrp_join(tid, departed, status, 1);
return ((error == EINVAL)? ESRCH : error);
}
/*
* pthread_join() differs from Solaris thr_join():
* It does not return the departed thread's id
* and hence does not have a "departed" argument.
* It returns EINVAL if tid refers to a detached thread.
*/
#pragma weak _pthread_join = pthread_join
int
pthread_join(pthread_t tid, void **status)
{
return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
}
They're even implemented using the same internal function.
But you don't want to use Solaris threads. Just use POSIX threads.

Does poll/epoll handling is in interrupt context?

This is probably trivial question for some people, but somehow I'm not sure about it.
When waiting with poll for event from kernel, is it that the handling of new event is done in interrupt context ?
If not, does it mean we can sleep/wait (using other commands in handler) in the handler ?
int main (void)
{
struct pollfd fds[2];
int ret;
fds[0].fd = FILENO;
fds[0].events = POLLIN;
fds[1].fd = FILENO;
fds[1].events = POLLOUT;
ret = poll(fds, 2, TIMEOUT * 1000);
if (ret == -1) {
perror ("poll");
return 1;
}
if (!ret) {
return 0;
}
if (fds[0].revents & POLLIN)
{
/********** HANDLING EVENTS HERE ***************/
printf ("FILENO is POLLIN\n");
}
if (fds[1].revents & POLLOUT)
{
/********** HANDLING EVENTS HERE ***************/
printf ("FILENO is POLLOUT\n");
}
return 0;
}
Thank you,
Ran
No (in general).
When you call poll(), the processor context switches to a kernel context, and other processes (and kernel threads) run. Your process will be context switched back in at some point after at least one of your FDs is ready. In general (consider for instance a pipe), interrupt context is not required for this, though note some I/O requires interrupt context to happen (not directly connected to poll()).

problem with posix threads c++

If i have 2 threads Thread1 and Thread2, but Thread2 will use some data that are procesed when Thread1 finishes.Is there a way for Thread2 to wait for Thread1 to finish and then get the data ?
If you need the data from thread1 and not just simple locking to prevent concurrent access, then you should use a semaphore:
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_post(sem_t *sem);
int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
int sem_destroy(sem_t *sem);
The main program runs sem_init before launching the threads. Thread 1 runs a sem_post to indicate it's done. Thread 2 uses a sem_wait to ensure Thread 1 is finished before it starts.
One way is to use condition vars:
Ex:
pthread_mutex_t mx;
pthead_cond_t cond;
void first_f(void *) {
...
pthread_mutex_lock(&mx)
// do something in first function
// the second function is waiting
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mx);
}
return NULL;
}
void second_f(void *) {
...
pthread_mutex_lock(&mx)
pthread_cond_wait(&cond, &mx);
// waiting for first function until we catch a signal
pthread_mutex_unlock(&mx);
}
return NULL;
}
The second way is to use two semaphores. First sem sets to 1, second set to zero. When the first function finishes, it sets first sem to 0, and second sem to 1. The second function waits until second semaphore is setted to 1 by first function and works.
It is called a mutex. pthreads calls the pthread_mutex, you should find them in the docs.
Yes, you can use a mutex to 'guard' the data in question:
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abs_timeout);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);

Can't get my thread to execute with SetEvent and WaitForSingleObject

I'm trying to create a thread and let it run until my main signals it to start, which I think is done with SetEvent. But the code in the thread is never executed. Below is the bare code I have stripped down of (I think) unrelated functions. Is the algorithm correct ?
Here is what I thought it did :
When in the main, the thread is created, which means it'll run in the background. When the event is set (SetEvent), the thread picks it up at WaitForSingleObject and then execute the code in the thread, right ?
HANDLE hThread;
HANDLE Event;
DWORD Thread()
{
while(1)
{
wait = WaitForSingleObject(Event, INFINITE)
//This is where I want to execute something
}
}
int _tmain()
{
DWORD dw;
int i;
Event = CreateEvent(NULL,false,false,NULL);
hThread = CreateThread(NULL,0,Thread,EventA,0,NULL);
while(1)
{
if (condition is correct)
{
SetEvent(Event);
}
CloseHandle(Thread);
CloseHandle(Event);
}
return 0;
}
Thanks for having read.
Move CloseHandle lines out of the while loop.

Resources