Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

how to transfer data between thread

An_N_1
New Contributor I
429 Views

I wrote a program using windows API to show some curves and text.

The calculation is a thread started by main window.

Curve and text's informations contains some data like position, color, strings....

I want to use windows message to send data to figure window. But message API function has two parameters: wParam, lParam, which refer to two integer pointer. I found it seems that fortran cannot transfer the adress of other type or struct to a integer, while an integer adress cannot be recieded by pointer of other types.

Is that right?

So, what should i do?

0 Kudos
4 Replies
andrew_4619
Honored Contributor II
429 Views

It would be easier to have the thread sharing the data, are they separate threads from the same program or or they in fact different processes?

To answer your question yes you can construct suitable W/l params in Fortran these depend on the message type, What is the message ID you are posting or sending? 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
429 Views

wParm and LParam are, for all intents and purposes, two (void*)'s. These arguments can contain either an intptr_t integer value or a pointer to something (you define the something). In Fortran these args would be INTEGER(C_PTR) types.

I agree with app4619 that if the two threads are in the same process then why use Windows Messaging? In this case use shared memory and construct your own Single Producer Single Consumer queue. This can be an array of pointers setup as a ring buffer. Producer inserts a pointer to array in the index of the fill pointer (provide that cell is ASSOCIATED), then bumps the index mod size of queue. Consumer waits for next empty index cell to be associated, copies pointer, nullifies cell in queue, advances empty index mode queue size, then processes data in the copy of the pointer. Sounds complicated but you should be able to get this done in under 10 lines of code each for fill function and empty function.

Jim Dempsey

0 Kudos
An_N_1
New Contributor I
429 Views

 app4619, jimdempseyatthecove:

Thanks for your reply!

The threads are in the same program in fact, and I thought using message can avoided to handle memory read/write lock. Perhaps that is not a proper idea...

I will try sharing memory methods, and could you give a advice on how to avoide the two thread not to read/write the same memory in the same time? Or that need not to concern about?

Another question is about fortran's pointer issue. If I made a user defined message such as WM_ShowText, I want to transfer a struct include position(x, y), and the text string. The calling thread may be can transfer the struct address by LOC() to argument wparam or Lparam, but I have not find a way to use the address as a struct pointer. In brief, the question is: when a varible's address(in integer) is know, how to set a same type pointer point to the address?

0 Kudos
An_N_1
New Contributor I
429 Views

 

If your app, or more precisely, any given queue is serviced by one producer thread and one consumer thread, this is called a Single Producer Single Consumer queue. This type of queue can be programmed with no requirement for lock (as outline in my earlier post). Multi-Producer and/or Multi-Consumer queues do require Lock(s) or Wait-Free programming techniques.

If (when) both threads are Fortran, then do not use LOC. Instead, when the objects in the queue are all the same, insert a Fortran pointer of that type into the queue. When you have a queue that requires different types, then define the queue as having pointers to polymorphic types and then use SELECT TYPE to qualify the extracted pointer.

This discussion should be placed in the open forum. As it is helpful for others in your same situation.

0 Kudos
Reply