Visual C++ :: Can Two Functions In Same DLL Be Called By Two Threads?

Aug 27, 2014

I write a DLL MyDLL.dll with Visual C++ 2008, as follows:

(1)MFC static linked
(2)Using multi-thread runtime library.

In the DLL, this is a global data m_Data shared by two export functions, as follows:

ULONGLONG WINAPI MyFun1(LPVOID *lpCallbackFun1) {
...
Write m_Data(using Critical section to protect)

[Code]....

Although MyThread1 and MyThread2 using critical section to protect the shared data m_Data, I will still suspend MyThread1 before accessing the shared data, to prevent any possible conflicts.

The problem is:

(1)When the first invoke of MyFun2, everything is OK, and the return value of MyFun2(that is nResult2) is 1 , which is expected.

(2)When the second, third and fourth invoke of MyFun2, the operations in MyFun2 are executed successfully, but the return value of MyFun2(that is nResult2) is a random value instead of the expected value 1. I try to using Debug to trace into MyFun2, and confirm that the last return statement is just return a value of 1, but the invoker will receive a random value instead of 1 when inspecting nResult2.

(3)After the fourth invoke of MyFun2 and return back to the next statement follow MyFun2, I will always get a "buffer overrun detected" error, whatever the next statement is.

I think this looks like a stack corruption, so try to make some tests:

1.I confirm the /GS (Stack security check) feature in the compiler is ON.

2.If MyFun2 is invoked after MyFun1 in MyThread1 is completed, then everything will be OK.

3.In debug mode, the codeline in MyFun2 that reads the shared data m_Data will not cause any errors or exceptions. Neither will the codeline in MyFun1 that writes the shared Data.

View 4 Replies


ADVERTISEMENT

C++ :: Create Two Threads Calling Even And Odd Functions

Mar 1, 2013

I want to create two threads which will be calling even and odd functions where even function should print even number and odd function should print odd number.Can it be possible with condition variable? What is the code in both the cases i.e. two separate function and with condition variable.

View 6 Replies View Related

C :: Write A Program With Two Functions Both Called From Main

Mar 14, 2013

Write a program with two functions both called from main(). The first function just prints "Hello". In the second function ask the user to enter a number. Calculate the square root of the number and return the result to main(). In main() print the square root value.

Code:

#include<stdio.h>
#include<math.h>
float fun3 (float );
main()
}

[code]...

View 3 Replies View Related

C++ :: Tracking From What Class Functions Called At Runtime

Feb 23, 2015

Is there any way to track what functions from what class are called at runtime? What I mean is a list of functions or classes which have been called at runtime.

View 1 Replies View Related

Visual C++ :: OnLButtonDown Not Getting Called On Slider Control In MFC?

Jun 20, 2013

I am using a slider control on a dialog box. I have handled OnLButtonDown for Slider control. But I wonder its not getting called whenever I click on the slider thumb, instead it gets called when I click anywhere on the dialog.

I want to handle OnLButtonDown when I click on the slider thumb.

View 6 Replies View Related

C :: Use SIGNALS To Control Threads

Nov 11, 2013

I've to use SIGNALS to control threads. Here's my code without the use of signals as I'm no good at that

Code:

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
}

[code]....

View 4 Replies View Related

C++ ::  threads To Take Sequential Turns

Jun 30, 2014

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int turn = 1;
int counter = 0;
int t, n;

[Code] ....

I found this program in a forum and it creates 1 to t threads with each thread pointing to the next and the last thread pointing to the first thread and it allows each thread to sequentially take a turn until all threads have taken n turns. That is when the program ends.

My doubt is if we see in void *tFunc(void *arg) function, first thread locks the mutex and then wait there forever till it's turn comes.Lets say if I spwan 1000 threads and they have take 10 turns each sequentially and what if a 550th thread locks the mutex as soon as the program starts and waiting for its turn which will never happen because still (turn=1) which means it's first thread's turn and first 549 threads have to complete their 1st turn before this 550 threda's turn comes.Doesnt this result in deadlock?

View 2 Replies View Related

C++ :: Encapsulate Two Threads In A Class?

Apr 29, 2014

how to encapsulate two threads in a class?

View 1 Replies View Related

C++ :: Stopping Threads Without Using Local Var?

Aug 31, 2014

Here's my code,

#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
void Blink();

[Code] .....

The problem is i don't want to use local vars , is ther anyway do end the loop safely(removing all the vars used in function and etc) without using a local var ?

View 3 Replies View Related

C/C++ :: Suspending And Resuming Threads

Apr 18, 2014

#include <stdio.h>
#include <pthread.h>
int i; // loop control variable
int *id; // array used to pass parameter of id
int nThreads; // #threads

[Code]....

I want to know where I am going wrong. I am strong in java but not in c so that may be my problem.

View 9 Replies View Related

C++ :: Creating And Terminating Threads?

Aug 31, 2014

I had a requirement where i needed to create a thread and if the execution of thread is not completed in 5 minutes i needed to terminate its execution and continue with other part of the code.

I used the below code to create the thread

_beginthread(FuncnCall,0,NULL);

HANDLE hThread = GetCurrentThread();

Then after this code, I used the below code to check for 5 minutes

for (int i=1;i<=0;i++) {
printf("Value of i=%d
",i);
if(threadFinished) {
break;
} else {
Sleep(1000);
}
}

After this if the value of "threadFinished" is false then i am terminating the thread like below

if(threadFinished == false) {
TerminateThread(hThread,0);
CloseHandle(hThread);
}

The Problem here is, after terminating the thread, the program abruptly closes by giving fatal error. Looks like memory leakage is happening after terminating the thread. Is it not the right way to safely exit the thread?

View 2 Replies View Related

C++ :: OpenMP - Intervals Assigned To Threads?

Feb 6, 2014

Is there a way of knowing which indices a thread is assigned in a parallel openMP scope?

View 6 Replies View Related

C :: Double Linked Structure Between 2 Threads

Jun 2, 2014

I am on a little project involving TCP socket-programming and multiple threads but need passing structures between two threads.

The first thread receives data with respective ID, temp, etc,.. Each ID is a list-item of a linked list and every ID has again a linked list of all the received data.

The second thread just have to access the list and perform some manipulations on it.

My solution: pass the head pointer of the linked list. Easy enough, right? Somehow, I am stuck receiving error message:" dereferencing pointer to incomplete type."

For the sake of ease and simplicity I just added a stripped down version of the project.

The magic (or not) happens in MainProcess.c: The Data thread should receive the pointer (a think) from the Connection thread.

View 5 Replies View Related

C++ :: Profiling Code - 4 Threads Slower Than 1

May 9, 2013

I am doing something wrong since for me 4 threads perform 2 times slower then 1.I have 2 vectors with bunch of data to process, there is no concurrency (not moving elements and are independent of each other) so i just need to calculate some data from one and copy result in another.

LARGE_INTEGER getFrequency()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return frequency;

[code].....

View 7 Replies View Related

C Sharp :: Stopping And Resuming Threads?

Jul 9, 2012

how do I stop and restart a Thread in C# once it reaches a certain location on a form then restart the thread once it goes outside of the location boundries?

I am trying to stop an animation thread (a bullet) once it reaches a block on a panel.

View 1 Replies View Related

C++ :: Threads Never Execute In Arbitrary Order?

Jul 25, 2014

Code:

void Arrival_MainThread::Body () {
t1 = new boost::thread((Arrive_Fctor()), 2);
t2 = new boost::thread((Arrive_Fctor()), 10000);
t3 = new boost::thread((Arrive_Fctor()), 3000);

Code:
class Arrive_Fctor {
public:
void operator()(int mean) {
m_Arrivals = new Arrivals(mean);
m_Arrivals->Activate();
#ifndef NO_RESOURCE

[code].....

The order of arrivals always appear as 3,2,1 it can't be 1,2,3 or 3,1,2 or something like that?

The body method is called when m_Arrival->Activate() is executed, and it is running as an independent thread.

View 4 Replies View Related

Visual C++ :: Mq4 Platform Calling Functions From DLL

Feb 10, 2013

I have Mq4 platform calling functions from DLL some how it doesnt make any differents when I make the call....

// DLL Code

#define WIN32_LEAN_AND_MEAN
#define MT4_EXPFUNC __declspec(dllexport)
using namespace std;
std::string My_String;
#define stringify(mymonths ) # mymonths

[Code] ....

I call this function

bool counts = StartRulls(bars);
Print("counts =",counts );
2013.02.09 23:03:242010.03.31 16:37 My_Mq4.mq4 EURCAD,M5: counts = 1

My result always stays 1

View 4 Replies View Related

Visual C++ :: Functions Were Discarded On Build Of DLL With 8.0

Dec 9, 2012

I want to build a dll library with visual c++ 8.0 and got an "unresolved symbol" linker error message when trying to use the library with an application. So I checked the dll library with dependency walker for the exported functions, the needed functions were missing in the library.

I then dumped the corresponding object file with dumpbin.exe, the function was declared as public, like the other exported functions. On the next step, I set the verbose switch for link.exe.

The verbose log told me, that the function was discarded on the linker run. What can lead to that behaviour of the visual c++ linker ?

View 3 Replies View Related

Visual C++ :: Const Member Functions

Jan 24, 2013

Code:
class Editor {
// c'tors etc
Gtk::EventBox canvas_event_box;
void functionA();
void functionB() const;

[Code] ....

When I try to compile functionB in Visual C++ it gives me this error:-

glibmm/refptr.h(199) : error C2440: 'initializing' : cannot convert from 'const Gdk::Window *' to 'Gdk::Window *'
Conversion loses qualifiers

And this is the code from glibmm/refptr.h

Code:
// The templated ctor allows copy construction from any object that's castable. Thus, it does downcasts:
// base_ref = derived_ref
template <class T_CppObject>
template <class T_CastFrom>

[Code] .....

I don't actually want to change anything in the member variable canvas_event_box. I just want to be able to call one of its functions from my 'const' member function. Is there any syntax I can use to tell VC++ that I'm not actually changing the variable - just using it.

View 11 Replies View Related

C++ :: Accessing And Working With Vector From Multiple Threads

Oct 20, 2014

I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.

The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.

However, if I need to switch from vectors to something else then I am open to that as well.

The following example below compiles but crashes rather quickly because it is not thread safe.

How I can fix the example below which I can then apply to my larger project?

#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {

[Code] ....

View 1 Replies View Related

C++ :: Solving Linear System Of Equations Using Threads

Dec 14, 2014

I am trying to write a code that solves a system of linear equations such as A*B=C. My system has a dimension equal to 1600. The matrix A cab be separated into 4 sub matrices and each can be handled by a different thread. I tried to solve this using the following code:

int main() {
int count = 0;
//Inputing matrix A
ifstream matrix;
matrix.open("example.txt");

[Code] ....

Although the above code gives the correct answer, the time needs to find the solution is bigger than that needed without using threads.

View 1 Replies View Related

C++ :: Reading / Writing File On Multiple Threads?

Aug 7, 2013

I'm currently working on a server for handling clients in a 2d online game and I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently, so I googled it and came up with
posts like

[URL]

I'm wondering if I can just treat it like everything else or will I have to do something specific for opening on multiple threads?

p.s. I did read those posts but I'm very new to multithreading

View 16 Replies View Related

C++ :: Passing Data Between Threads Without Using Global Variables

Nov 6, 2013

I have a main thread and a worker thread. How do i pass data between them when both are already running without using global variables?

View 1 Replies View Related

Visual C++ :: String Passing Between Functions In SDI Application?

Apr 8, 2013

I am doing an project in SDI. I have two functions name sendtext(CString str) and displaytext(CString inr) both in different class. I have a pointer name pView to send the string str to function "displaytext". The problem is after some operations i get a text in str and i send that text to display text in the output screen i get the text and wen the second text comes to "displaytext" the former text disappears and the latest string only present. How can i display both the text in the output window on ClistCtrl class.

Code:
Void sendtext(CString str)
{
// do some operations
................
pView->displaytext(str);
}
void displaytext(CString inr) {
CListCtrl &ctlsde = this->GetListCtrl();
ctlsde.InsertColumn(1, _T("First "), LVCFMT_LEFT, 80);
int nItem;
nItem = ctlsde.InsertItem(0, inr);
}

View 14 Replies View Related

Visual C++ :: Const Char Returned By Various Functions

May 13, 2013

What is the programmers responsibility with respect to const char * returned by various functions, like the C++ string class c_str() function which returns a const char * to an c style string array? In VC++ I cannot delete a const char * which holds a string literal. Take the following code for example:

Code:
void func() //a useless function with illustrative code {
string s1("abcd");
string s2("efgh");
const char * cc1 = s1.c_str(); //c_str() returns a const char * c style string pointer
s2.c_str(); //this returns a const char *, which must be allocated on the heap right?
delete cc1; //produces run time error in Release mode in VC++
}

The problem with the above code snip is that space is allocated on the heap (or so I believe) for the const char *'s returned by the 2 calls to c_str(). The delete attempt fails and there is no opportunity to delete the space allocated by const char * because its not assigned to anything (however I see c_str() used this way extensively)

So, if I cannot delete a const char *, how does the memory get recovered? Perhaps the string objects s1 and s2 themselves have pointers to the items on the heap made by c_str() calls and they get deleted by the destructors of s1 and s2 when the function ends?

View 3 Replies View Related

Visual C++ :: Convert Unicode (UTF-16) Functions To Ansi (UTF-8)

Nov 20, 2012

Working in Win32 console app (VS 2010) I have been trying to convert several Unicode (UTF-16) C++ functions to Ansi C (UTF-8). The test app includes two tokenizer classes, each of which work perfectly well in their respective environments, CTokA and CTokW (UTF-8 and UTF-16).

A problem arises when I attempt to run the UTF-8 functions when the Character Set properties is set to 'Use Unicode Character Set' in that std::string manipulations do not perform as expected, e.g.,

printf("start
");

gets reproduced as

printf("start
");══════════ ²²²²

Attempting to null terminate the string where it is supposed to end simply results in a space in that position and the garbage end persists, e.g.,

printf("sta t
");══════════ ²²²²

Code:
sline[11] = 0x0000;

If I attempt to change the Character Set property to 'Use Multibyte Character Set' or 'Not Set', the app will not compile and hundreds of errors occur. Of course, I can eliminate all of the UTF-16 code, but it strikes me that it should not be necessary. Perhaps if M$ made everything UTF-16 without all of the necessary decorations like 'L' and '_T(', life would be much simpler. Unfortunately, I have a very extensive UTF-8 app under 10 years of development that works quite well, but my UTF-16 (Unicode) conversion doesnt work as well because of the mixing of pointers (I think), so I have had to revert much of the code back to UTF-8. (All of which has nothing to do with my question but is simply psychotheraputic for me to ventilate on.)

My question is this: Can UTF-8 and UTF-16 code coexist in a single Win32 console app?

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved